$(function () {
    // 查询列表
    querySearch()

    // 查询枚举类
    $.get(baseURL + "logistics/erplogistics/enumlist",
        function(r){
        vm.e.logisticsSendTypeEnumList = r.map.logisticsSendTypeEnumList;
        vm.e.logisticFinanceStatusList = r.map.logisticFinanceStatusList;
        vm.e.merchantList = r.map.merchantList;
        vm.logisticsTypeEntityList = r.map.logisticsTypeEntityList;
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        dialogFormVisible: false,
        costFormVisible: false,
        page:{
            currPage:1, // 当前页
            pageSize:20, // 分页数
            totalCount:0, // 总条数
            tableData:[], //数据列
        },
        q:{
            orderNo:"",
            logisticsName:"",
            sellerId:"",
            beginCreatetime:"",
            endCreatetime:"",
            logiNo:"",
            logisticFinanceStatus:"",
            beginLogiCost:"",
            endLogiCost:""
        },
        e:{ //枚举类
            logisticsSendTypeEnumList:[],
            logisticFinanceStatusList:[],
            merchantList:[]
        },
        form: {
            logiId: '',
            logiPaytime: '',
            logiPayRemark: '',
            logiCost: null,
        },
        costForm: {
            logiId:'',
            logiCost: '',
            logisticsName:'',
            logiNo:'',
            logisticsId:''
        },
        logiIds: [],
        logisticsTypeEntityList:[]//物流名称集合
    },
    methods: {
        query: function () {
            vm.reload();
        },
        dateFormat:function(date) {
            var year=date.getFullYear();
            var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
            var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
            var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
            var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
            var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
            return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        },
        update: function (event) {
            var goodsId = getSelectedRow();
            if(goodsId == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(goodsId)
        },
        getInfo: function(goodsId){
            $.get(baseURL + "goods/goods/info/"+goodsId, function(r){
            });
        },
        reset:function(){
            vm.q.orderNo= '';
            vm.q.logisticsName= '';
            vm.q.thirdMerchant='';
            vm.q.logiNo='';
            vm.q.beginCreatetime="";
            vm.q.endCreatetime="";
            vm.q.logisticFinanceStatus="";
            vm.q.beginLogiCost="";
            vm.q.endLogiCost="";
        },
        reload: function (event) {
            querySearch()
        },
        /* 订单起始 */
        checkBeginDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }})
        },
        /* 订单结束 */
        checkEndDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false, onpicking:function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }})
        },
        /*分页数改变*/
        handleSizeChange(val) {
            vm.page.pageSize = val;
            querySearch();
        },
        /*分页当前页改变改变*/
        handleCurrentChange(val) {
            vm.page.currPage = val
            querySearch();
        },
        /*格式化 送货类型*/
        formatSendType: function (row, column, cellValue, index) {
            for(var i=0; i<vm.e.logisticsSendTypeEnumList.length; i++){
                if(cellValue == vm.e.logisticsSendTypeEnumList[i].id){
                    return vm.e.logisticsSendTypeEnumList[i].describe
                }
            }
        },
        /*格式化 审核状态*/
        formatFinanceStatus: function (row, column, cellValue, index) {
            for(var i=0; i<vm.e.logisticFinanceStatusList.length; i++){
                if(cellValue == vm.e.logisticFinanceStatusList[i].id){
                    return vm.e.logisticFinanceStatusList[i].describe
                }
            }
        },
        /*审核*/
        audit: function(index, row) {
            vm.logiIds = [];
            vm.logiIds.push(row.logiId);
            vm.batchAudit();
        },
        /* 付款页面 */
        payView: function (row) {
            vm.logiIds = [];
            vm.logiIds.push(row.logiId);
            vm.form.logiCost = row.logiCost;
            vm.dialogFormVisible = true;
        },
        /* 付款 */
        payConfirm: function() {
            var logiPaytime = vm.form.logiPaytime;
            if (isBlank(logiPaytime)){
                alert("付款日期不能为空");
                return;
            }
            var postData = {
                logiId: vm.logiIds.join(","),
                logiPaytime: vm.dateFormat(logiPaytime),
                logiPayRemark: vm.form.logiPayRemark
            }
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/batchPayConfirm",
                contentType: "application/json",
                data: JSON.stringify(postData),
                success: function(r){
                    if(r.code == 0){
                        alert("操作成功！");
                        vm.dialogFormVisible = false;
                        querySearch();
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        /* 批量付款 */
        batchPayment: function () {
            if (vm.logiIds.length == 0){
                alert("请选择一条记录");
                return;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/logiCostCount",
                contentType: "application/json",
                data: JSON.stringify(vm.logiIds),
                success: function(r){
                    if(r.code == 0){
                        vm.form.logiCost = r.totalCost;
                        vm.dialogFormVisible = true;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        /* 弃审 */
        back: function(index, row) {
            this.$confirm('确认弃审?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                    type: "GET",
                    url: baseURL + "logistics/erplogistics/back?logiId="+row.logiId,
                    contentType: "application/json",
                    data: null,
                    success: function(r){
                        if(r.code == 0){
                            alert("操作成功！");
                            querySearch();
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消弃审'
                });
            });
        },
        /* 删除物流信息 */
        deleteLogi: function (logiId) {
            this.$confirm('确认删除物流信息吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        $.ajax({
                            type: "GET",
                            url: baseURL + "logistics/erplogistics/deleteLogi?logiId=" + logiId,
                            contentType: "application/json",
                            data: null,
                            success: function(r){
                                if(r.code === 0){
                                    querySearch();
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        /*录入成本*/
        enteringCost: function() {
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/enteringCost",
                contentType: "application/json",
                data: JSON.stringify(vm.costForm),
                success: function(r){
                    if(r.code == 0){
                        alert("操作成功！");
                        vm.costFormVisible = false;
                        querySearch();
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleRowSelection(row);
            });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            vm.logiIds = [];
            for (var i in val){
                vm.logiIds.push(val[i].logiId);
            }
        },

        /* 批量审核 */
        batchAudit: function () {
            if (vm.logiIds.length == 0){
                alert("请选择一条记录");
                return;
            }

            this.$confirm('确认审核通过?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    type: "POST",
                    url: baseURL + "logistics/erplogistics/batchAudit",
                    contentType: "application/json",
                    data: JSON.stringify(vm.logiIds),
                    success: function(r){
                        if(r.code == 0){
                            alert("操作成功！");
                            vm.logiIds = [];
                            querySearch();
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消审核'
                });
            });
        }
    }
});

// 列表查询
function querySearch() {
    var queryData = {
        "limit":vm.page.pageSize,
        "page":vm.page.currPage,
        "orderNo":vm.q.orderNo,
        "logisticsName":vm.q.logisticsName,
        "sellerId":vm.q.sellerId,
        "logiNo":vm.q.logiNo,
        "beginCreatetime":vm.q.beginCreatetime,
        "endCreatetime":vm.q.endCreatetime,
        "logisticFinanceStatus":vm.q.logisticFinanceStatus,
        "beginLogiCost":vm.q.beginLogiCost,
        "endLogiCost":vm.q.endLogiCost,
    }
    $.ajax({
        type: "GET",
        url: baseURL + "logistics/erplogistics/list",
        contentType: "application/json",
        data: queryData,
        success: function(r){
            vm.page.tableData = r.page.list
            vm.page.totalCount = r.page.totalCount
        }
    });
}
